<?php
/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */

namespace app\admin\controller;
use think\facade\View;

use app\common\logic\ModuleLogic;
use think\facade\Db;
use think\facade\Cache;

use app\common\model\Category as CategoryModel;

use app\common\model\Order as CommonModel;
//use app\admin\validate\Order as validateInfo;

class Order extends Base
{
    function list(){
        if(IS_AJAX){
            $model = new CommonModel();
            $params = input('param.');
            $res = $model->tableData($params);
            return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);
        }else {
            $param = input('param.');
            $module = 'info';

            //获取栏目列表
            $pay_status = config("app.pay_status");


            View::assign('pay_status',$pay_status);
            View::assign('module',$module);
            return View::fetch();
        }
    }

    public function add(){
        $param = input('param.');
        $model = new CommonModel();
        if(IS_POST){
            $validate = new validateInfo;
            $result = $validate->scene('add')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $res = $model -> handle($param);
            return dyajaxReturn($res['code'],$res['msg'],$param);
        }else {

            $module = 'info';
            //获取栏目列表
            $CategoryModel = new CategoryModel();
            $treeList = $CategoryModel->treeList([]);
            View::assign('treeList',$treeList);

            View::assign('cate_id',$param['cate_id']);
            View::assign('module',$module);

            View::assign('title','轮播图');
            View::assign('add_time',date('Y-m-d'));

            $sort = $model->max("orderid");
            View::assign('sort',$sort+1);

            return View::fetch();
        }
    }


    public function edit(){
        $param = input('param.');
        $model = new CommonModel();
        if(IS_POST){
            $param = input('param.');

            $validate = new validateInfo;
            $result = $validate->scene('edit')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $res = $model -> handle($param);
            return dyajaxReturn($res['code'],$res['msg'],$param);
        }else{
            $id = $param['id'];
            $info = $model->where('id','=',$id)->find();

            $module = 'info';
            //获取栏目列表
            $CategoryModel = new CategoryModel();
            $treeList = $CategoryModel->treeList([]);
            View::assign('treeList',$treeList);

            View::assign('cate_id',$param['cate_id']);
            View::assign('module',$module);

            View::assign('id',$id);
            View::assign('info',$info);
            View::assign('title','轮播图');
            return View::fetch();
        }
    }

    //删除
    public function delete(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];

        $res = DB::name($table)->where($id_name,'=',$id)->delete();
        if(!$res){
            return dyajaxReturn(0,'删除失败');
        }
        return dyajaxReturn(1,'删除成功');
    }


    //状态改变
    public function changeStatus(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];
        $field = $param['field'];
        $checked = $param['checked'];

        if($checked == 'true'){
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>1]);
        }else{
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>2]);
        }
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        return dyajaxReturn(1,'操作成功');
    }

    //展示
    public function show(){
        $param = input('param.');
        $id = $param['id'];

        $model = new CommonModel();
        $info = $model->info($id);
        View::assign('info',$info);
        return View::fetch();
    }

}